<?php

/**
 * adminlocations actions.
 *
 * @package    retailbrain
 * @subpackage adminlocations
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminlocationsActions extends sfActions {

    /**
     * Executes locations action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Locaciones');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Locaciones", "adminlocations/index"));
        
        $q = Doctrine_Query::create()
                ->from('Locations l')
                ->orderBy('l.name desc');
        
        $this->locations = $q->fetchArray();
        
        $this->caps = $this->getUser()->getAttribute('caps');
    }

    
    /**
     * Executes editLocations action
     *
     * @param sfRequest $request A request object
     */
    public function executeEditLocations(sfWebRequest $request) {

        $id = $this->getRequestParameter('id');

        $this->getResponse()->setSlot('title', 'Administración : Editar Locación');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Locaciones", "adminlocations/index"), array("Editar Locación", "adminlocations/editLocations?id=$id"));
        
        
        /** Locationes **/
        $q = Doctrine_Query::create()
                ->from('Locations l')
                ->where('l.id_location = ?', array($id))
                ->fetchArray();

        $this->location = $q;
        
    }
    
    
    /**
     * Executes addLocations action
     *
     * @param sfRequest $request A request object
     */
    public function executeAddLocations(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Agregar Locación');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Locaciones", "adminlocations/index"), array("Agregar Locación", "adminlocations/addLocations"));
        
    }
    
    
    /**
     * Executes createLocation action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreateLocation(sfWebRequest $request) {


        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('locations');
        
        try {

            $tabla = Doctrine::getTable('Locations');
            $tabla = new Locations();

            $tabla->name        = strtoupper($_arrayPost['name']);
            
            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }


        $this->redirect('/adminlocations/index');
    }
    
    
    /**
     * Executes updateLocation action
     * 
     * @param sfWebRequest $request
     * @return type 
     */
    public function executeUpdateLocation(sfWebRequest $request) {

        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('locations');
        
        try {

            $tabla = Doctrine::getTable('Locations')->find(array($_arrayPost["id"]));

            $tabla->name        = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $this->redirect('/adminlocations/index');
    }
    
    
    
    /**
     * LocationActive
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateActiveLocation(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $active = $request->getParameter('active');
        $active = (($active == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Locations l')
                ->set('l.active', '?', $active)
                ->where('l.id_location = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Locations l')
                ->orderBy('l.name desc');
        
        $this->locations = $q->fetchArray();
        
        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstLocations');
    }
    
    /**
     * LocationDelete
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateDeleteLocation(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $deleted = $request->getParameter('deleted');
        $deleted = (($deleted == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Locations l')
                ->set('l.deleted', '?', $deleted)
                ->where('l.id_location = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Locations l')
                ->orderBy('l.name desc');
        
        $this->locations = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstLocations');
    }
}
